<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增车辆信息')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-manage-add">
        <div class="col-md-6">
            <div class="form-group">
                <label class="col-sm-3 control-label">编号：</label>
                <div class="col-sm-9">
                    <input type="text" name="" class="form-control" placeholder="" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">车辆名称：</label>
                <div class="col-sm-8">
                    <input name="carName" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">创建人：</label>
                <div class="col-sm-9">
                    <input name="createPerson" class="form-control" type="text"
                           th:value="${@permission.getPrincipalProperty('userName')}" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">创建时间：</label>
                <div class="col-sm-9">
                    <input id="createTimeInput" type="text" name="" class="form-control" placeholder="" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">司机：</label>
                <div class="col-sm-8">
                    <select id="driverSelect" name="driverId" class="form-control m-b">
                        <option value="">请选择司机</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <div class="form-group">
                <label class="col-sm-3 control-label">状态：</label>
                <div class="col-sm-8">
                    <select name="carStatus" class="form-control m-b"
                            th:with="type=${@dict.getType('category_status')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">车牌号：</label>
                <div class="col-sm-8">
                    <input name="carCard" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">颜色：</label>
                <div class="col-sm-8">
                    <select name="carColor" class="form-control m-b" th:with="type=${@dict.getType('car_color')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                th:value="${dict.dictValue}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">手机号：</label>
                <div class="col-sm-9">
                    <input id="phoneNumberInput" type="text" name="" class="form-control" placeholder="手机号码" readonly>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">附件：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="file">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="file" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-9">
                    <input type="text" name="" class="form-control" placeholder="请输入文本">
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script th:inline="javascript">
    var prefix = ctx + "car/manage"
    $("#form-manage-add").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-manage-add').serialize());
            // $.operate.save(prefix + "/addRecord", $('#form-manage-add').serialize());
        }
    }

    $(".file-upload").fileinput({
        uploadUrl: ctx + 'common/upload',
        maxFileCount: 1,
        autoReplace: true
    }).on('fileuploaded', function (event, data, previewId, index) {
        $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
    }).on('fileremoved', function (event, id, index) {
        $("input[name='" + event.currentTarget.id + "']").val('')
    })
</script>

<script>
    // 获取当前时间
    var now = new Date();

    // 格式化时间
    var year = now.getFullYear();
    var month = (now.getMonth() + 1).toString().padStart(2, '0');
    var day = now.getDate().toString().padStart(2, '0');
    var hour = now.getHours().toString().padStart(2, '0');
    var minute = now.getMinutes().toString().padStart(2, '0');
    var second = now.getSeconds().toString().padStart(2, '0');

    // 构造时间字符串
    var currentTime = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;

    // 将时间字符串显示在输入框中
    document.getElementById('createTimeInput').value = currentTime;
</script>

<script>
    $(document).ready(function () {
        $.ajax({
            url: prefix + '/getUserList',
            type: 'GET',
            dataType: 'json',
            success: function (response) {
                // 获取到用户列表后，填充到下拉框中
                var userList = response.data;
                var selectElement = $('#driverSelect');
                $.each(userList, function (index, user) {
                    selectElement.append($('<option>', {
                        value: user.userId,
                        text: user.userName
                    }));
                });
            },
            error: function (xhr, status, error) {
                console.error('错误', error);
            }
        });
    });

    // 监听下拉框的选择事件
    $('#driverSelect').change(function () {
        var selectedUserId = $(this).val();
        $.ajax({
            url: prefix + '/getUserPhoneNumber',
            type: 'GET',
            dataType: 'json',
            data: {
                userId: selectedUserId
            },
            success: function (response) {
                if (response.code === 0) {
                    var phoneNumber = response.data.phonenumber;
                    $('#phoneNumberInput').val(phoneNumber);
                } else {
                    console.error('获取用户信息失败');
                }
            },
            error: function (xhr, status, error) {
                console.error('错误', error);
            }
        });
    });
</script>


</body>
</html>